package com.aiton.bamin.bamindaijiadrier.activity;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.aiton.administrator.shane_library.shane.upgrade.Upgrade;
import com.aiton.administrator.shane_library.shane.upgrade.UpgradeUtils;
import com.aiton.administrator.shane_library.shane.utils.ACache;
import com.aiton.administrator.shane_library.shane.utils.GsonUtils;
import com.aiton.administrator.shane_library.shane.utils.HTTPUtils;
import com.aiton.administrator.shane_library.shane.utils.VersionUtils;
import com.aiton.administrator.shane_library.shane.utils.VolleyListener;
import com.aiton.administrator.shane_library.shane.widget.TwoBtnDialog;
import com.aiton.bamin.bamindaijiadrier.R;
import com.aiton.bamin.bamindaijiadrier.constant.Constant;
import com.aiton.bamin.bamindaijiadrier.server.LocationService;
import com.android.volley.VolleyError;

public class SettingActivity extends ZjbBaseActivity implements View.OnClickListener {

    private LocationService.MyBind mMyBind;
    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mMyBind = (LocationService.MyBind) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    private TextView mTv_setting_versionName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        init();
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initViews() {
        //获取版本号
        String currVersionName = VersionUtils.getCurrVersionName(this);
        if (TextUtils.isEmpty(currVersionName)) {
            mTv_setting_versionName.setText("V1.0.0");

        } else {
            mTv_setting_versionName.setText("V"+currVersionName);

        }
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.button_exit).setOnClickListener(this);
        findViewById(R.id.imageView_back).setOnClickListener(this);
        findViewById(R.id.rela_update).setOnClickListener(this);
        findViewById(R.id.rela_change_password).setOnClickListener(this);
        findViewById(R.id.rela_charges_notes).setOnClickListener(this);
        findViewById(R.id.rela_clause).setOnClickListener(this);
        findViewById(R.id.rela_about).setOnClickListener(this);
    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {

    }

    @Override
    protected void findID() {
        mTv_setting_versionName = (TextView) findViewById(R.id.tv_setting_versionName);

    }


    @Override
    public void onClick(View v) {
        Intent intent = new Intent();

        switch (v.getId()) {
            case R.id.imageView_back:
                finishTo();
                break;
            case R.id.button_exit:
                final TwoBtnDialog twoBtnDialog = new TwoBtnDialog(this, "确定要退出吗？", "确定", "取消");
                twoBtnDialog.setClicklistener(new TwoBtnDialog.ClickListenerInterface() {
                    @Override
                    public void doConfirm() {
                        //停止接单
                        mMyBind.clearLocation();
                        twoBtnDialog.dismiss();
                        ACache aCache = ACache.get(SettingActivity.this);
                        aCache.clear();
                        Intent intent = new Intent();
                        intent.setClass(SettingActivity.this, LoginActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivityTo(intent);
                        finishTo();
                    }
                    @Override
                    public void doCancel() {
                        twoBtnDialog.dismiss();
                    }
                });
                twoBtnDialog.show();
                break;
            case R.id.rela_update://检查更新
                checkUpdate();
                break;
            case R.id.rela_charges_notes://收费说明
                intent.setClass(this,ChargesNotesActivity.class);
                intent.putExtra(Constant.INTENT_KEY.TITLE,"收费说明");
                intent.putExtra(Constant.INTENT_KEY.URL,Constant.Url.CHARGES_NOTES);
                startActivityTo(intent);
                break;
            case R.id.rela_clause://法律条款
                intent.setClass(this,WebViewActivity.class);
                intent.putExtra(Constant.INTENT_KEY.TITLE,"法律条款");
                intent.putExtra(Constant.INTENT_KEY.URL,Constant.Url.CLAUSE);
                startActivityTo(intent);
                break;
            case R.id.rela_change_password://修改密码
                intent.setClass(this,ChangePasswordActivity.class);
                intent.putExtra(Constant.INTENT_KEY.TYPE,Constant.TYPE.LOGIN);
                startActivityTo(intent);
                break;
            case R.id.rela_about://关于
                intent.setClass(this, AboutActivity.class);
                startActivityTo(intent);
                break;
        }
    }
    /**
     * 检查更新
     *
     */
    private void checkUpdate() {
        HTTPUtils.get(SettingActivity.this, Constant.Url.UP_GRADE, new VolleyListener() {
            public void onResponse(String json) {
                Upgrade mUpgrade = GsonUtils.parseJSON(json, Upgrade.class);
                int currVersion = VersionUtils.getCurrVersion(SettingActivity.this);
                if (mUpgrade.getVersion() > currVersion) {
                    new AlertDialog.Builder(SettingActivity.this)
                            .setTitle("升级")
                            .setMessage(mUpgrade.getFeature())
                            .setPositiveButton("升级",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            if (ContextCompat.checkSelfPermission(SettingActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                                                    != PackageManager.PERMISSION_GRANTED) {
                                                //申请WRITE_EXTERNAL_STORAGE权限
                                                ActivityCompat.requestPermissions((Activity) SettingActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, Constant.PERMISSION.PERMISSION_WRITE_EXTERNAL_STORAGE_SSENGJI);
                                            } else {
                                                UpgradeUtils.checkUpgrade(SettingActivity.this, Constant.Url.UP_GRADE);
                                            }
                                        }
                                    }).setNegativeButton("取消", null).show();
                } else {
                    Toast.makeText(SettingActivity.this, "当前已是最新版本！", Toast.LENGTH_SHORT).show();
                }
            }


            @Override
            public void onErrorResponse(VolleyError arg0) {
                Log.e("SettingActivity", "检查更新失败"  );

            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        Intent server = new Intent();
        server.setClass(SettingActivity.this, LocationService.class);
        bindService(server, conn, Context.BIND_AUTO_CREATE);
        startService(server);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unbindService(conn);
    }
}
